#!/bin/bash

CLUSTER_INFO=`grep -v localhost  /etc/hosts | awk '{print("server."NR"="$2":2888:3888")}'`

# 利用hosts文件批量zk集群
PORT=2181
INDEX=1
#>>>>>>>>>>>>>>>>>>>>>> 取hosts中主机开始 >>>>>>>>>>>>>>>>>>>>>>>>>>>>
USER=`whoami`
SELF_HOST_NAME=$(hostname)
# 统一所有主机脚本执行目录
CURRENT_PATH=$PWD
#IFS设置IFS环境变量，这个变量是用来定义分隔符类型的。默认的分隔符为空格、换行符、tab。
IFS_old=$IFS
IFS=$'\n'
for host in `grep -v localhost  /etc/hosts | awk '{print($2)}'`; do
    echo "echo >>> $host 主机上 zookeeper 端口为：$PORT"
    echo "echo >>> 切换路径：$CURRENT_PATH"

    ssh $host "cd $CURRENT_PATH;$PWD/zookeeper.sh $PORT;echo $INDEX > /opt/module/zookeeper/apache-zookeeper-3.6.0-bin/data/myid;echo $CLUSTER_INFO >> /opt/module/zookeeper/apache-zookeeper-3.6.0-bin/conf/zoo.cfg"
    #端口递增2181 2182 2183 ....
    PORT=`expr $PORT + 1`;
    INDEX=`expr $INDEX + 1`;
done
#将IFS变量还原为默认值
IFS=$IFS_old
#>>>>>>>>>>>>>>>>>>>>>> 取hosts中主机结束 >>>>>>>>>>>>>>>>>>>>>>>>>>>>
echo $CLUSTER_INFO